/**
 * @author zhounan 
 * @date 2024年7月30日
 * @version V1.0
 */
package org.zn.etl.controller;

import java.time.Duration;

import javax.annotation.PostConstruct;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.logging.LoggingMeterRegistry;
import io.micrometer.core.instrument.logging.LoggingRegistryConfig;

/**
 * @author zhounan 
 * @date 2024年7月30日
 * @version V1.0
 */

@RestController
@RequestMapping("test/meter")
public class MeterRegistryController {
	
	private	LoggingMeterRegistry mr;
	
	@PostConstruct
	private void init() {
		
		LoggingRegistryConfig config=new LoggingRegistryConfig() {
            @Override
            public String get(String key) {
                return "test.meter";
            }
            @Override
            public Duration step() {
                return Duration.ofSeconds(30);
            }
            
            @Override
            public Duration connectTimeout() {
            	// TODO Auto-generated method stub
            	return Duration.ofMinutes(3l);
            }
            
            @Override
            public Duration readTimeout() {
            	// TODO Auto-generated method stub
            	return Duration.ofSeconds(60L);
            }
            
            @Override
            public int batchSize() {
            	// TODO Auto-generated method stub
            	return  1000;
            }
            @Override
            public int numThreads() {
            	// TODO Auto-generated method stub
            	return 4;
            }
            @Override
            public boolean logInactive() {
            	// TODO Auto-generated method stub
            	return true;
            }
            
            @Override
            public boolean enabled() {
            	// TODO Auto-generated method stub
            	return true;
            }

        };
		

		mr=new LoggingMeterRegistry(config,Clock.SYSTEM);
		
	}
	
	
	@GetMapping("count")
	public Object test() {
		
		 Counter.builder("metrics.request.count").tags("request", "test").register(mr).increment();
		
		 return mr.get("metrics.request.count").counter().count();
	}

	
	@GetMapping("get")
	public Object get() {
			
		 return mr.get("metrics.request.count").counter().count();
	}
	
}
